/* 
    处理标签的逻辑
*/
const { sqlQuery } = require("../mysql/index");
const { responseMsg } = require("../utils");

module.exports = {
  getSignList,
  addSign,
};

// 获取所有标签
async function getSignList() {
  let sql = `select * from label_data;`;
  return new Promise(function (resolve, reject) {
    sqlQuery(
      sql,
      (data) => {
        resolve(responseMsg(data, "success"));
      },
      (err) => {
        reject(responseMsg(err, "error"));
      }
    );
  });
}

// 新增标签
async function addSign(data) {
  let name = data.name;

  let sql = `insert into label_data(name) values("${name}");`;
  return new Promise(function (resolve, reject) {
    sqlQuery(
      sql,
      (data) => {
        const newId = data?.insertId;
        resolve(responseMsg({ id: newId }, "success"));
      },
      (err) => {
        console.log("error:", err);
        reject(responseMsg(err, "error"));
      }
    );
  });
}
